<?php

namespace Module;

class AdminModule extends BaseModule {

    public function indexAction() {
        $error = "";
        if(isset($_POST['username']) && isset($_POST['password'])){
            $login = $_POST['username'];
            $password = $_POST['password'];
            if($user = \Lib\Model\UserModel::checkAuth($login, $password)){
                $_SESSION['user'] = $login;
                $_SESSION['roles'] = $user['roles'];
                return $this->showTemplate(__FUNCTION__, array(), '');
            } else {
                $error = 'Неправильный логин или пароль';
            }
        }

        if(!\Lib\Model\UserModel::isAdmin()){
            return $this->showTemplate(__FUNCTION__, array('error' => $error), '', '_not_admin');
        }
        return $this->showTemplate(__FUNCTION__, array(), '');
    }
    
    public function allAction() {
        
        if(!\Lib\Model\UserModel::isAdmin()){
                header( 'Location: /hide/admin/', true, 301 );
        }
        
        $posts = \Lib\Model\PostModel::getLatest();
        return $this->showTemplate(__FUNCTION__, array('posts' => $posts), '');
    }
    
    public function newAction() {
        if(!\Lib\Model\UserModel::isAdmin()){
                header( 'Location: /hide/admin/', true, 301 );
        }
        
        return $this->showTemplate(__FUNCTION__, array(), '');
    }
    
}

?>
